package com.evolved.automata.android.bots.speechbot;
import java.util.*;

abstract public class MapTransformAdapter implements MapTransformer
{
	String triggerKey;
	String triggerValue;
	
	Hashtable<String,Boolean> keysToAdd;
	Hashtable<String,Boolean> keysToRemove;
	boolean overwriteExistingP;
	Hashtable<String, String> newMap;
	
	public MapTransformAdapter(String triggerKey, String triggerValue, boolean overwriteP)
	{
		this.triggerKey=triggerKey;
		this.triggerValue=triggerValue;
		keysToAdd= new Hashtable<String, Boolean>();
		keysToRemove = new Hashtable<String, Boolean>();
		overwriteExistingP=overwriteP;
		initialize();
	}
	
	public void initialize()
	{
		
	}
	
	public Hashtable<String, String> transformMap(Hashtable<String, String> map)
	{
		newMap = (Hashtable<String, String>)map.clone();
		String keyValue;
		for (String baseKey:map.keySet().toArray(new String[0]))
		{
			keyValue = map.get(baseKey);
			if (baseKey.equals(triggerKey)||keyValue.equals(triggerValue))
			{
				newMap.remove(baseKey);
				doTransformMap();
			}
			
		}
		
		return newMap;
	}
	
	private void doTransformMap()
	{
		String newValue=null;
		for (String addedKey:getKeysToAdd())
		{
			if (!newMap.containsKey(addedKey)||overwriteExistingP)
			{
				newValue=processKeyValue(addedKey);
				newMap.put(addedKey, newValue);
			}
		}
		
		for (String removedKey:getKeysToRemove())
		{
			if (newMap.containsKey(removedKey)&&overwriteExistingP)
			{
				newMap.remove(removedKey);
			}
		}
		
	}
	
	
	public abstract String processKeyValue(String keyName);
	
	
	
	public abstract String[] getKeysToAdd();
	
	
	public abstract String[] getKeysToRemove();

	
}
